<?php


namespace App\Http\Controllers\Admin;


use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;

class GradePopularController extends BaseController
{
    /**
     * 获取等级列表
     */
    public function GradePopularList(Request $request)
    {
        $param = $request->all();
        $v = app('validator')->make($param, [
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $list = app('repo_grade_config')->GradePopularList($param);
        return Response::sendData($list);
    }

    /**
     * 等级新增
     */
    public function rankPopularAdd(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'grade' => 'required',
                'experience' => 'required|numeric',
                'screen_img' => 'string|max:255',
            ],
            [
                'grade.required' => '等级不能为空',
//                'grade.numeric' => '等级必须为数字',
                'experience.required' => '经验值不能为空',
                'experience.numeric' => '经验值必须为数字',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        if (strlen($request->input('grade')) > 11) {
            throw new BasicException(10001, '等级字符长度不能大于11位');
        }
        if (strstr($request->input('experience'), '.')) {
            if (strlen($request->input('experience')) > 12) {
                throw new BasicException(10001, '经验值字符长度不能大于12位');
            }
        } else {
            if (strlen($request->input('experience')) > 10) {
                throw new BasicException(10001, '经验值字符长度不能大于10位');
            }
        }
        $grade = $request->input('grade');
        $experience = $request->input('experience');
        $screen_img = $request->input('screen_img','');
        //数据组合
        $param['level']=$grade;
        $param['exp']=$experience;
        $param['icon'] = $screen_img;
        $param['created_at'] = time();
        //执行添加操作
        app('repo_grade_config')->rankPopularAdd($param);

        return Response::sendData(true);
    }

    /**
     * 等级修改
     */
    public function rankPopularSave(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric',
//                'grade' => 'required|numeric',
                'experience' => 'required|numeric',
            ],
            [
                'grade.required' => '等级不能为空',
//                'grade.numeric' => '等级必须为数字',
                'experience.required' => '经验值不能为空',
                'experience.numeric' => '经验值必须为数字',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        if (strlen($request->input('grade')) > 11) {
            throw new BasicException(10001, '等级字符长度不能大于11位');
        }
        if (strstr($request->input('experience'), '.')) {
            if (strlen($request->input('experience')) > 12) {
                throw new BasicException(10001, '经验值字符长度不能大于12位');
            }
        } else {
            if (strlen($request->input('experience')) > 10) {
                throw new BasicException(10001, '经验值字符长度不能大于10位');
            }
        }
        $id = $request->input('id');
        $grade = $request->input('grade');
        $experience = $request->input('experience');
        $screen_img = $request->input('screen_img');

        //数据组合
        $param['level']=$grade;
        $param['exp']=$experience;
        $param['icon'] = $screen_img;
        //执行添加操作
        app('repo_grade_config')->rankPopularSave($id,$param);

        return Response::sendData(true);
    }

    /**
     * 删除人气等级
     * @param Request $request
     */
    public function rankPopularDel(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'ids' => 'required',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $ids = $request->input('ids');
        $param = explode(',', $ids);
        //执行添加操作
        app('repo_grade_config')->rankPopularDel($param);
        return Response::sendData(true);
    }

    /**
     * 人气等级详情
     * @param Request $request
     * @return string
     * @throws BasicException
     */
    public function rankPopularDetail(Request $request)
    {
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $id = $request->input('id');
        //执行查询操作
        $detail = app('repo_grade_config')->rankPopularDetail($id);
        return Response::sendData($detail);
    }
}